<?php

namespace App\Http\Controllers\Admin;

use App\Goods;
use App\GoodsType;
use App\Http\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsTypeController extends Controller
{
    use Response;
//    protected $mch_id;
//    public function __construct()
//    {
//        $this->mch_id=auth('merchant')->id();
//    }

    public function index(Request $request)
    {
        $data=GoodsType::all();
        return $this->success(200,$data);
    }

    public function show($id)
    {
        $data=GoodsType::find($id);
        return $this->success(200,$data);
    }
    public function store(Request $request)
    {
        $created=GoodsType::create($request->all());
        if ($created){
            return $this->message(200,'添加成功');
        }else{
            return $this->error(505,'添加失败');
        }
    }
    public function update($id,Request $request)
    {
        $update=GoodsType::where('id',$id)->update($request->all());
        if ($update){
            return $this->message(200,'修改成功');
        }else{
            return $this->error(505,'修改失败');
        }
    }
    public function destroy($id)
    {
        $goods=Goods::where('goods_type_id',$id)->get()->toArray();
        if (count($goods)>0){
            return $this->message(505,'无法修改');
        }
        $delete=GoodsType::destroy($id);
        if($delete){
            return  $this->message(200,'删除成功');
        }else{
            return $this->message(505,'删除失败');
        }
    }
}
